<?php

/**
 * 发布品牌周报-每周一上午10点发布
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandWeeklyReport;
use Biz\model\brand\Brand as mBrand;
use Biz\model\brandAccount\BrandAccountInfo;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\LinkFormat;
use Biz\service\BrandRights\BrandEquityVerificationService;

//按是否有权益发送周报
$page = 1;
$pageSize = 100;
$sendBrandIds = [];//发送周报的品牌id
$noRightsBrandIds = []; //没有权益的品牌id
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $brandList = mBrand::selectByWhere([
        'uid[>]' => 0,
        'status' => mBrand::STATUS_USING,
        'brand_type' => [
            mBrand::BRAND_TYPE_COMMON,
            mBrand::BRAND_TYPE_BEST,
            mBrand::BRAND_TYPE_SUPER,
        ],
        'pack_type[>]' => 0,
        'LIMIT' => $limit,
        'ORDER' => ['id' => 'ASC']
    ],
        ['id', 'uid']
    );
    if ($brandList) {
        $brandList = array_column($brandList, null, 'id');
        $brandIds = array_column($brandList, 'id');
        $day = date('Y-m-d');
        $startDay = date('Y-m-d', strtotime("-30 days", strtotime($day)));
        $accountList = BrandAccountInfo::getAllByWhere([
            'type' => BrandAccountInfo::ACCOUNT_TYPE_BRAND,
            'account_status' => BrandAccountInfo::ACCOUNT_STATUS_USED,
            'brand_id' => $brandIds,
            'in_start_time[<=]' => $day,
            'in_end_time[>=]' => $day
        ], ['brand_id', 'in_start_time', 'in_end_time']);
        $accountList = $accountList ? array_column($accountList, null, 'brand_id') : [];
        foreach ($accountList as $row) {
            $brandInfo = isset($brandList[$row['brand_id']]) ? $brandList[$row['brand_id']] : [];
            if ($brandInfo && $brandInfo['id'] && $brandInfo['uid']) {
                if (BrandEquityVerificationService::checkOneSourceStatus($row['brand_id'],'data_weekly',true)) {
                    sendMsg($brandInfo['uid']);
                    $sendBrandIds[] = $row['brand_id'];
                } else {
                    $noRightsBrandIds[] = $row['brand_id'];
                }

            }
        }
    }
    $page++;
} while ($brandList);

var_dump('已发送周报的品牌:' . count($sendBrandIds) . '#' . json_encode($sendBrandIds));
var_dump('没有数据周报权益的品牌:' . count($noRightsBrandIds) . '#' . json_encode($noRightsBrandIds));


function sendMsg($uid)
{
    $weeklyDay = BrandWeeklyReport::getWeeklyDay();
    if ($uid) {
        $link = Hhzurl::getDomain() . '/brand/weekly?weekly_day=' . $weeklyDay;
        $link = LinkFormat::encryptionLink($link);
        $data = [
            'msgCode' => 0,
            'to_uid' => $uid,
            'msgParams' => [
                'title' => '',
                'detail' => '叮！本周品牌周报来啦，等你来看',
                'img' => 'https://img.haohaozhu.cn/Op-imageShow/7c15d20go0840DA00qudlzn?iv=1',
                'link' => $link,
                'type' => 2,
            ]
        ];
        \Doraemon\cache\Queue::insertQueue('queue/Sysmsg/Send', $data, \Doraemon\cache\Queue::QUEUE_DORAEMON);
    }
}